<template>
	<view class="perInfo">
		<view class="main">
			<view class="items" @tap="changeHead">
				<view class="label">头像</view>
				<view class="val" >
					<view class="portrait" >
						<!-- <image src="../../../../static/tabbar/mine.png" mode=""></image> -->
						<image class="img" :src="info.face?info.face:defaultFace" mode=""></image>
					    <u-icon name="arrow-right"></u-icon>	
					</view>
				</view>
			</view>
			<view class="items"  @tap="navTo('昵称',info.nickName)">
				<view class="label">昵称</view>
				<view class="val">{{info.nickName}}<u-icon name="arrow-right"></u-icon></view>
			</view>
			<view class="items" @tap.stop="selectSexHandle">
				<view class="label">性别</view>
				<view class="val" >
				<text>{{info.sexLabel}}</text>
				<u-icon name="arrow-right"></u-icon></view>
			</view>
			<view class="items" @tap="dateShow = true">
				<view class="label">出生年月</view>
				<view class="val">
					<!-- <u-input placeholder="请选择" input-align="right" v-model="info.birthday" :clearable="false" style="height:90rpx"
						>
					</u-input> -->
					<view class="val" >{{info.birthday}}</view>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="items" @tap="navTo('身高',info.height)">
				<view class="label">身高</view>
				<view class="val" >{{info.height}}cm<u-icon name="arrow-right"></u-icon></view>
			</view>
			<view class="items" @tap="navTo('体重',info.weight)">
				<view class="label">体重</view>
				<view class="val" >{{info.weight}}kg<u-icon name="arrow-right"></u-icon></view>
			</view>
			<view class="items" @tap="navTo('运动目的',info.targetType)">
				<view class="label">运动目的</view>
				<view class="val" >{{info.target}}<u-icon name="arrow-right"></u-icon></view>
			</view>
			<view class="items">
				<view class="label">身体状况</view>
				<view class="val">
					<text v-if="BMI < 18.5" style="color: #F69B30" >消瘦</text>
					<text v-if="18.5 <= BMI && BMI < 23.9" style="color: #64A888" >正常</text>
					<text v-if="24 <= BMI && BMI < 27.9" style="color: #EB3839" >超重</text>
					<text v-if="28 <= BMI && BMI < 31.9" style="color: #EB3839">Ⅰ型肥胖</text>
					<text v-if="32 <= BMI && BMI < 35.9" style="color: #EB3839">Ⅱ型肥胖</text>
					<text v-if="BMI > 36" style="color: #EB3839">Ⅲ型肥胖</text>
				</view>
			</view>
			<view class="items">
				<view class="label">BMI指数</view>
				<view class="val">{{BMI}}</view>
			</view>
			<view class="items">
				<view class="label">健康建议</view>
				<view class="val">{{BMI | formatHealthy}}</u-icon></view>
			</view>
			<!-- <view class="dd" @tap="texts">
				确定
			</view> -->
		</view>
		<!-- 头像选择弹出框 -->
		<u-popup v-model="headShow" @close="close" mode="bottom" class="select-head">
			<view>
				<view class="head-row" @tap="chooseHead(1)">
					<text>相册</text>
				</view>
				<view class="head-row" @tap="chooseHead(2)">
					<text>拍照</text>
				</view>
				<view class="cancel" @tap="close">
					<text>取消</text>
				</view>
			</view>
		</u-popup>
		<!-- 性别选择弹出框 -->
		<u-popup v-model="show" @close="close" mode="bottom" class="select-sex">
			<view>
				<view class="sex-row" @tap="chooseHandle('男')">
					<text>男</text>
				</view>
				<view class="sex-row" @tap="chooseHandle('女')">
					<text>女</text>
				</view>
				<view class="cancel" @tap="close">
					<text>取消</text>
				</view>
			</view>
		</u-popup>
		<!-- 时间弹出框 -->
		<view>
			<u-picker v-model="dateShow" :params="date" @confirm="dateConfirm"></u-picker>
		</view>
	</view>
</template>

<script>
	import {API} from '../../../../config/myApi.js';
	import { lk_buyer,lk_common,lk_expert,lk_seller,lk_manager,lk_consumer } from '../../../../config/myConfig.js';
	export default {
		data() {
			return {
				defaultFace: '../../../../static/tabbar/mine.png',
		        sexLabel: '',
				sex: '',
				params: {
					face: '',
					nick: '',
					sex: '',
					birthday: '',
					height: '',
					// mobilePhone: '',
					nickName: '',
					weight: '',
					targetType: '',
				},
				headShow: false,
				//性别
				show: false,
				//日期
				dateShow: false,
				date: {
					year: true,
					month: true,
					day: true,
				},
				info: {},
				BMI: "",
			};
		},
		onLoad() {
			this.getType();
		},
		filters:{
			formatBMI(v) {
				let res = "";
				if (v < 18.5) res = "消瘦";
				if (8.5 <= v && v < 23.9) res = "正常";
				if (24 <= v && v < 27.9) res = "超重";
				if (28 <= v && v < 31.9) res = "Ⅰ型肥胖";
				if (32 <= v && v < 35.9) res = "Ⅱ型肥胖";
				if (v > 36) res = "Ⅲ型肥胖";
				return res;
			},
			formatHealthy(v) {
				let res = "";
				console.log(v)
				if (v < 18.5) res = "您的体型属于消瘦	建议您通过力量练习和多增加营养摄入，增加体重。";
				if (18.5 <= v && v < 23.9) res = "您的体型属于正常	建议您继续保持良好的饮食习惯和锻炼习惯，保持正常体型。";
				if (24 <= v && v < 27.9) res = "您的体型属于超重	建议您适当控制饮食摄入，增加快走、慢跑、球类等有氧运动，减少身体脂肪含量。";
				if (28 <= v && v < 31.9) res = "您的体型属于Ⅰ型肥胖	建议您合理控制饮食摄入，增加快走、慢跑、球类等有氧运动，减少身体脂肪含量。";
				if (32 <= v && v < 35.9) res = "您的体型属于Ⅱ型肥胖	建议您合理控制饮食摄入，增加快走、慢跑、球类等有氧运动，减少身体脂肪含量。";
				if (v > 36) res = "您的体型属于Ⅲ型肥胖	建议您合理控制饮食摄入，增加快走、慢跑、球类等有氧运动，减少身体脂肪含量。";
				return res;
			},
		},
		methods: {
			switchBack() {
				console.log('dddff')
				uni.switchTab({
					url: '/pages/tabbar/user/my'
				})
			},
			navTo(label,val){
			    let url = '/pages/tabbar/user/personalCenter/perEdit'
			    let param = {
		            label: label,
		            val: val
		        }
			    this.$navTo.navTos(url,param)
			},
			// getUserInfo() {
			// 	// const data = uni.getStorageSync('userDetail')
			// 	API.getMember().then(res=> {
			// 		console.log('ddd',res)
			// 		this.info = res.result;
			// 	})
			// },
			saveEdit(lab,val) {
				let pram= {};
				if(lab=='head') {
					pram = {};
					pram.face = val;
				}
				if(lab=='sex') {
					pram = {}
					pram.sex = val;
				}
				if(lab=='birth') {
					pram = {}
					pram.birthday = val;
				}
				API.editOwn(pram).then(res=> {
					// console.log('editOwn',res)
					if(res.code==200) {
						this.$u.toast('保存成功');
					}
				})
				// address, birthday, face, nickName, region, regionId, sex
			},
			
			// 获取运动目的
			getType() {
				const token = uni.getStorageSync('token');
				const pram = {
					name: "",
					page: {
					    order: "",
					    pageNumber: 1,
					    pageSize: 9,
					    sort: ""
					},
					typeIds: [1000]
				}
				// const info = uni.getStorageSync('userDetail');
				API.getMember().then(res=> {
					// this.info = res.result;
					const info = res.result;
					let target;
					if(info.targetType) {
						target = info.targetType.split(',');
					}
					info.sex=='1'?info.sexLabel ='男':info.sexLabel ='女'
					API.getDataList(pram).then(res => {
						if(res.code==200) {
							const data = res.result.records;
							for (let item of data) {
								item.select=false;
							}
							// this.targetList = data;
							data[0].select=true
							const temp = [];
							if(info.targetType) {
								for (let item of data) {
									for (let k of target) {
										if(item.id==k) {
											info.name=item.name
											temp.push(item.name);
										}
									}
								}
								info.target = temp.join(' ')
							}
							this.info = info;
						  this.BMI = this.getBMI(info);
							console.log(this.info)
							
						}
					})
				})
			},
			// 获取BMI
			getBMI(info) {
				let w = info.weight ? parseFloat(info.weight) : 0;
				let h = info.height ? parseFloat(info.height) / 100 : 1;
				console.log(info.weight,'zzzzzzz')
				return  parseFloat(w / (h * h), 2).toFixed(2);
			},
			changeHead() {
				//更换head地址
				this.headShow = true;
			},
			chooseHead(code) {
				//code为1调用相册,code为2调用相机
				if (code == 1) {
					uni.chooseImage({
						count: 1,
						sizeType: ['original', 'compressed'],
						sourceType: ['camera', 'album'], //这要注意，camera掉拍照，album是打开手机相册
						success: (res) => {
							// console.log(res);
							const tempFilePaths = res.tempFilePaths;
							// this.info.face = res.tempFilePaths[0]
							// 图片文件上传
							uni.uploadFile({
								url: `${lk_common}/common/common/upload/file`,
								filePath: res.tempFilePaths[0],
								name: 'file',
								header: {
								   "accessToken": uni.getStorageSync('token'),
							    },
								success: (res) => {
									// const data = res.data.split(',');
									
									const data = JSON.parse(res.data);
									this.info.face = data.result;
									console.log('上传后吃饭成功',data.result)	;								
									this.saveEdit('head', this.info.face)
								},
							});
							// this.$forceUpdate();
							this.headShow = false;
						}
					});
				} else if (code == 2) {
					uni.chooseImage({
						count: 1,
						sizeType: ['original', 'compressed'],
						sourceType: ['camera', 'album'], //这要注意，camera掉拍照，album是打开手机相册
						success: (res) => {
							// console.log(res);
							const tempFilePaths = res.tempFilePaths;
							this.info.face = res.tempFilePaths[0]							
							this.saveEdit('head', this.info.face)
							this.headShow = false;
						}
					});
				}
				// API.
			},
			//弹出框的关闭
			close() {
				this.show = false;
				this.headShow = false;
			},
			selectSexHandle(e) {
				this.show = true;
				// console.log('ggg',e);
			},
			//弹出框选择性别
			chooseHandle(sex) {
				this.info.sexLabel=sex;
				if(sex==="男") {
					this.sex = '1';
				}else {
					this.sex = '0';
				}
				this.info.sex = this.sex
				console.log('sss',sex,this.info.sex)
				this.saveEdit('sex',this.sex);
				this.show = false;
				
			},
			//呼出日期选择器
			chooseDate() {
				this.dateShow = true;
			},
			//选择日期后自动填入
			dateConfirm(val) {
				this.info.birthday = val.year + '-' + val.month + '-' + val.month;
				this.saveEdit('birth',this.info.birthday);
				console.log('srsrsr',val,this.info.birthday);
			},
			//监听身高发生改变，进行处理
			heightHandle(val){
				if(val < 0){
					this.params.height = "";
				}else if(val > 300){
					this.params.height = 300;
				}
			},
			
			//监听身高体重发生改变，进行处理
			weightHandle(val){
				if(val < 0){
					this.params.weight = "";
				}else if(val > 200){
					this.params.weight = 200;
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '../../aless/personalInformation.scss';
</style>
